#include "bOS/bOSString.h"
using namespace bOS::CoreString;

#include "WAPI/WAPI.h"
#include "WAPI/WAPIActivity.h"
#include "WAPI/WAPIWorkItem.h"
#include "WAPI/WAPIProcess.h"

#include "DataModel/Activity.h"

#include "CodeManager/Tracer.h"

int main(int argc, char* argv[])
{
	CM_SETTING_TO("WFExecutor", 7);
	CM_CODEBLOCK(main)

	if ( argc == 1 )
	{
		cout << "Usage: WorkflowName [NrTime]" << endl;
		exit(1);
	}

	String sWorkflowName;
	if ( argc >= 2 )
	{
		sWorkflowName = argv[1];
	}

	unsigned int uiSize =1;
	if ( argc == 3 )
	{
		uiSize = atoi(argv[2]);
	}

	cout << "Press a Key for beginning................................................" << endl;
	getchar();

	Response* response = new Response();
	response->iCode= 0;

	cout << "InitSession......." << endl;
	InitSession(response);
	if ( response->iCode != 0 ) 
	{
		cout << "Initializing [KO].Error[" << response->sMsg << "]. Exit." << endl;
		return 1;
	}
	cout << "Sessione Initialized." << endl;
	
	
	long lProcessId;
	char* acRet = NULL;
	
	for ( unsigned int i=0; i<uiSize; i++)
	{
		createWorkflowProcess((char*)sWorkflowName.c_str(), lProcessId, response);
		if ( response->iCode == 0 )
		{
			cout << "WorkflowProcess[" << lProcessId << "]. Created" << endl;
			startProcessInstance(lProcessId, response);
			if ( response->iCode == 0 )
			{
				cout << "WorkflowProcess[" << lProcessId << "]. started" << endl;
			}
			else
			{
				cout << "WorkflowProcess[" << lProcessId << "]. NOT STARTED" << endl;
			}
		}
		else
		{
			cout << "Error creating Workflow.[" << response->sMsg << "]" << endl;
		}
	}

	unsigned long lWorkflowRunning;
	do
	{
		lWorkflowRunning= getNumberRunningWorkflow(response);
		if ( response->iCode == 0 )
		{
			cout << "WorkflowRunning[" << lWorkflowRunning << "]" << endl;
		}
		else
		{
			cout << "Error retreiving RunningWorkflow.Err[" << response->sMsg << "]" << endl;
		}
	}
	while ( lWorkflowRunning > 0);

	cout << "Press a Key for terminating................................................" << endl;
	getchar();


	exit(1);
	cout << "Restarting Manuale" << endl;
	getchar();

	DataManager::Panorama::WorkflowProcesses::Activity activity;
	activity.setName("Manuale");
	activity.setProcessId (++lProcessId);
	acquireActivity((char*) activity.getXMLString(0).c_str(), "ddematteo", response);
	doWorkItem((char*) activity.getXMLString(0).c_str(), "ddematteo", response);
	completeWorkItem((char*) activity.getXMLString(0).c_str(), "ddematteo","Accept", response);

	do
	{
		lWorkflowRunning= getNumberRunningWorkflow(response);
		if ( response->iCode == 0 )
		{
			cout << "WorkflowRunning[" << lWorkflowRunning << "]" << endl;
		}
		else
		{
			cout << "Error retreiving RunningWorkflow.Err[" << response->sMsg << "]" << endl;
		}
	}
	while ( lWorkflowRunning > 0);

	cout << "EndSession......." << endl;
	response->iCode = 0;
	EndSession(response);
	if ( response->iCode == 0 ) 
	{
		cout << "Session Terminated [OK]" << endl;
	}
	else 
	{
		cout << "Terminating [KO].Err[" << response->sMsg << "]" << endl;
	}

	

	return 0;
}

